package com.topnav.mc.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.DeviceChannelMapper;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.DeviceChannel;
import com.topnav.mc.admin.entity.bean.DeviceChannelBean;
import com.topnav.mc.admin.entity.param.DeviceChannelParam;
import com.topnav.mc.admin.service.IDeviceChannelService;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.admin.service.IEntityOperateLogService;
import com.topnav.mc.base.mc.McServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

@Service
//public class DeviceChannelServiceImpl extends CommServiceImpl<DeviceChannelMapper, DeviceChannel> implements IDeviceChannelService {
public class DeviceChannelServiceImpl extends McServiceImpl<DeviceChannelMapper, DeviceChannel, DeviceChannelBean, DeviceChannelParam>
        implements IDeviceChannelService {
    @Autowired
    private DeviceChannelMapper deviceChannelMapper;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IEntityOperateLogService entityOperateLogService;

    protected void afterDelete(DeviceChannel entity){
        //this.updateChannelCount(entity);
        entityOperateLogService.saveLog(entity,"DeviceChannel","REMOVE");
    };
    protected void afterUpdate(DeviceChannel entity){
        //this.updateChannelCount(entity);
        entityOperateLogService.saveLog(entity,"DeviceChannel","EDIT");
    };

    protected void afterSave(DeviceChannel entity){
        //this.updateChannelCount(entity);
        entityOperateLogService.saveLog(entity,"DeviceChannel","ADD");
    };
    @Override
    public boolean beforeUpdate(DeviceChannel entity){
        if (StringUtils.isEmpty(entity.getChannelCode())){
            throw new CustomException(-1,"通道号必须输入");
        }
        QueryWrapper<DeviceChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("channel_code",entity.getChannelCode());
        wrapper.eq("device_id",entity.getDeviceId());
        wrapper.ne("id",entity.getId());
        DeviceChannel deviceChannel = super.getOne(wrapper,false);
        if (deviceChannel != null){
            throw new CustomException(-1,"此通道号已存在");
        }

        entity.setUpdateTime(new Date());
        return true;

    }
    @Override
    public boolean beforeSave(DeviceChannel entity){
        if (StringUtils.isEmpty(entity.getChannelCode())){
            throw new CustomException(-1,"通道号必须输入");
        }

        QueryWrapper<DeviceChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("channel_code",entity.getChannelCode());
        wrapper.eq("device_id",entity.getDeviceId());
        DeviceChannel deviceChannel = super.getOne(wrapper,false);
        if (deviceChannel != null){
            throw new CustomException(-1,"此通道号已存在");
        }

        if (StringUtils.isEmpty(entity.getName())){
            entity.setName(entity.getChannelCode());
        }
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());

        return true;
    }

    @Override
    public DeviceChannel getChannelFromDevAndChnCode(Long deviceId,String ChannelCode){
        QueryWrapper<DeviceChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",deviceId).eq("channel_code",ChannelCode);
        return super.getOne(queryWrapper,false);
    }

    @Override
    public <E extends IPage<DeviceChannel>> E page(E page, Wrapper<DeviceChannel> queryWrapper) {
        QueryWrapper<DeviceChannel> wrapper = new QueryWrapper<DeviceChannel>();
        if (queryWrapper.getEntity() != null) {
            if (queryWrapper.getEntity().getDeviceId()!=null){
                wrapper.eq("c.device_id", queryWrapper.getEntity().getDeviceId());
            }
        }
        E e = getBaseMapper().selectPage(page, wrapper);
        return e;
    }






}
